package com.hejia.bulbul.controller;

import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import javax.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import com.hejia.alauda.entity.BankInfo;
import com.hejia.alauda.entity.BankMaintain;
import com.hejia.alauda.entity.example.BankMaintainExample;
import com.hejia.alauda.service.PaymentService;
import com.hejia.alauda.utils.Pager;
import com.hejia.bulbul.entity.DataTablesReturnObject;
import com.hejia.bulbul.entity.JSONParam;

@Controller
@RequestMapping("bankmaintain")
public class BankMaintainController extends BaseController {

	@Resource
	private PaymentService paymentService;

	@Resource
	private PaymentService bankInfoService;

	/**
	 * 列表
	 */
	@RequestMapping(value = "bankmaintainlist", method = RequestMethod.GET)
	public String bankmaintainList(Pager<BankMaintain> pager, ModelMap model) {
		Pager<BankMaintain> bankmaintain = paymentService
				.findAllBankMaintain(pager);
		model.put("bankmaintain", bankmaintain);
		return "/setting/bankmaintain_list";
	}

	/**
	 * 跳转到添加界面
	 */
	@RequestMapping(value = "add", method = RequestMethod.GET)
	public String addBankMaintainList(ModelMap model) {
		List<BankInfo> banklist = bankInfoService.findAllBankInfo();
		model.put("banklist", banklist);
		return "/setting/bankmaintain_input";
	}

	/**
	 * 添加完保存
	 * 
	 * @throws ParseException
	 */
	@RequestMapping(value = "add", method = RequestMethod.POST)
	public String saveBankMaintain(BankMaintain bankMaintain, ModelMap model,
			RedirectAttributes redirectAttributes) throws ParseException {
		if (paymentService.saveBankMaintain(bankMaintain)) {
			redirectAttributes.addFlashAttribute("status", STATUS_SUCCESS);
			redirectAttributes.addFlashAttribute("msg", "银行维护添加成功");
		} else {
			redirectAttributes.addFlashAttribute("status", STATUS_FAILD);
			redirectAttributes.addFlashAttribute("msg", "银行维护添加失败");
		}

		return "redirect:/bankmaintain/bankmaintainlist";
	}

	/**
	 * 跳转到修改界面
	 */
	@RequestMapping(value = "update/{id}", method = RequestMethod.GET)
	public String updateBankMaintain(@PathVariable Long id, ModelMap model) {
		BankMaintain bankMaintain = paymentService.getBankMaintainById(id);
		List<BankInfo> banklist = bankInfoService.findAllBankInfo();
		model.put("banklist", banklist);
		model.put("bankmaintain", bankMaintain);
		return "/setting/bankmaintain_input";
	}

	/**
	 * 修改完保存
	 */
	@RequestMapping(value = "update/{id}", method = RequestMethod.POST)
	public String saveUpdateBankMaintain(@PathVariable Long id,
			BankMaintain bankMaintain, RedirectAttributes redirectAttributes) {

		if (paymentService.updateBankMaintain(bankMaintain)) {
			redirectAttributes.addFlashAttribute("status", STATUS_SUCCESS);
			redirectAttributes.addFlashAttribute("msg", "银行维护修改成功");
		} else {
			redirectAttributes.addFlashAttribute("status", STATUS_FAILD);
			redirectAttributes.addFlashAttribute("msg", "银行维护修改失败");
		}

		return "redirect:/bankmaintain/bankmaintainlist";
	}

	/**
	 * 删除
	 */
	@RequestMapping(value = "delete/{id}", method = RequestMethod.GET)
	public String deleteBankMaintain(@PathVariable Long id,
			RedirectAttributes redirectAttributes) {
		if (paymentService.deleteBankMaintain(id)) {
			redirectAttributes.addFlashAttribute("status", STATUS_SUCCESS);
			redirectAttributes.addFlashAttribute("msg", "银行维护删除成功");
		} else {
			redirectAttributes.addFlashAttribute("status", STATUS_FAILD);
			redirectAttributes.addFlashAttribute("msg", "银行维护删除失败");
		}

		return "redirect:/bankmaintain/bankmaintainlist";
	}

	/**
	 * 以ajax格式返回列表请求
	 *
	 * @param params
	 * @return
	 */
	@RequestMapping(value = "bankmaintainAsAjax", method = RequestMethod.POST)
	@ResponseBody
	public DataTablesReturnObject<BankMaintain> bankmaintainAsAjax(
			@RequestBody JSONParam[] params) {
		HashMap<String, String> paramMap = JSONParam.convertToMap(params);

		// 分页信息
		Pager<BankMaintain> pager = new Pager<BankMaintain>();
		int start = Integer.parseInt(paramMap.get("iDisplayStart"));
		int length = Integer.parseInt(paramMap.get("iDisplayLength"));
		pager.setPageSize(length);
		pager.setPageNumber(start / length + 1);

		// 查询条件
		BankMaintainExample exp = new BankMaintainExample();

		String sSearch = paramMap.get("sSearch");
		if (StringUtils.isNotEmpty(sSearch)) {
			exp.appendCriterion("bankName like  ", "%" + sSearch + "%");
		}
		// 排序
		int iSortingCols = Integer.parseInt(paramMap.get("iSortingCols"));
		for (int i = 0; i < iSortingCols; i++) {
			String iSortCol = paramMap.get("iSortCol_" + i);
			String mDataProp = paramMap.get("mDataProp_" + iSortCol);
			String sSortDir = paramMap.get("sSortDir_" + i);
			if (i > 0) {
				exp.setOrderByClause(exp.getOrderByClause() + ",");
				exp.setOrderByClause(exp.getOrderByClause() + mDataProp + " "
						+ sSortDir);
			} else {
				exp.setOrderByClause(mDataProp + " " + sSortDir);
			}
		}

		pager = paymentService.findAllBankMaintain(pager, exp);

		DataTablesReturnObject<BankMaintain> dtr = new DataTablesReturnObject<BankMaintain>(
				pager.getTotalCount(), paramMap.get("sEcho"), pager.getList());
		return dtr;
	}

}
